import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 马拉圈
 * Date: 2023-03-12
 * Time: 12:58
 */
public class Test {

    public static void createNewOne(Connection connection) throws SQLException {
        String useSql = "use newOne";
        PreparedStatement preparedStatement = connection.prepareStatement(useSql);
        preparedStatement.executeUpdate();
        preparedStatement.close();

        String searchSql = "show tables like \"Student\"";
        preparedStatement = connection.prepareStatement(searchSql);
        ResultSet set = preparedStatement.executeQuery();
        if(set.next()) {
            System.out.println("已有此表");
        }else{
            preparedStatement.close();
            String sql = "create table Student (id int, name varchar(20))";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.executeUpdate();
            System.out.println("创建成功");
        }
        set.close();
        preparedStatement.close();
        connection.close();
    }
    public static void main(String[] args) throws SQLException {
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/" +
                /*这里java107含义是数据库名*/"java107?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("密码");
        Connection connection = dataSource.getConnection();
        String sql = "show databases like \"newOne\"";//查询性质的语句
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        ResultSet set = preparedStatement.executeQuery();
        if(set.next()) {
            System.out.println("数据库已存在");
        }else{
            System.out.println("创建数据库");
            preparedStatement.close();
            String createSql = "create database newOne charset = utf8";
            preparedStatement = connection.prepareStatement(createSql);
            preparedStatement.executeUpdate();
        }
        createNewOne(connection);
        set.close();
        preparedStatement.close();
        connection.close();
    }

    public static void main1(String[] args) throws SQLException {
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/" +
                /*这里java107含义是数据库名*/"java107?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("密码");
        Connection connection = dataSource.getConnection();

        String sql = "insert into classes (id, name) values (?,?)";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);

        Scanner scanner = new Scanner(System.in);
        int id = scanner.nextInt();
        String name = scanner.nextLine();
        preparedStatement.setInt(1, id);
        preparedStatement.setString(2, name);
        System.out.println(preparedStatement.executeUpdate());

        String searchSql = "select id, name from classes";
        preparedStatement.close();
        preparedStatement = connection.prepareStatement(searchSql);
        ResultSet set = preparedStatement.executeQuery();
        while(set.next()) {
            id = set.getInt("id");
            name = set.getString("name");
            System.out.println(id + " " + name);
        }

        set.close();
        preparedStatement.close();
        connection.close();
    }
}
